package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.ICopAttachementService;
import com.ibeeking.found.cop.b.rest.service.IMsgFansMomentSopDetailService;
import com.ibeeking.found.cop.b.rest.service.IMsgFansMomentSopService;
import com.ibeeking.found.cop.service.common.bo.MsgSendRuleBO;
import com.ibeeking.found.cop.service.common.dos.*;
import com.ibeeking.found.cop.service.common.enums.WxCpMessageAssignStatusEnum;
import com.ibeeking.found.cop.service.common.param.CopAttachementParam;
import com.ibeeking.found.cop.service.common.param.MsgFansMomentSopDetailParam;
import com.ibeeking.found.cop.service.common.param.MsgFansMomentSopParam;
import com.ibeeking.found.cop.service.common.param.UniAttachParam;
import com.ibeeking.found.cop.service.common.query.MsgFansMomentSopPageQuery;
import com.ibeeking.found.cop.service.common.query.MsgFansMomentSopQuery;
import com.ibeeking.found.cop.service.common.vo.CopAttachementVO;
import com.ibeeking.found.cop.service.common.vo.MsgFansMomentSopDetailVO;
import com.ibeeking.found.cop.service.common.vo.MsgFansMomentSopVO;
import com.ibeeking.found.cop.service.common.vo.UniAttachVO;
import com.ibeeking.found.cop.service.mapper.*;
import com.ibeeking.found.id.GenerateId;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.date.DateUtil;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName MsgFansMomentSopServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class MsgFansMomentSopServiceImpl extends ServiceImpl<MsgFansMomentSopMapper, MsgFansMomentSopDO> implements IMsgFansMomentSopService {

    private static String DATE_FORMAT_YYYYMMDD = "yyyy-MM-dd";

    private static String DATE_FORMAT_HHMMSS = "HH:mm:ss";

    @Resource
    private MsgFansMomentSopMapper msgFansMomentSopMapper;

    @Resource
    private MsgFansMomentSopDetailMapper msgFansMomentSopDetailMapper;

    @Resource
    private IMsgFansMomentSopDetailService msgFansMomentSopDetailServiceImpl;

    @Resource
    private CopAttachementMapper copAttachementMapper;

    @Resource
    private ICopAttachementService copAttachementServiceImpl;

    @Resource
    private CopEmployeeMapper copEmployeeMapper;

    @Resource
    private FriendsMapper friendsMapper;

    @Resource
    private FriendsTagsRelMapper friendsTagsRelMapper;

    @Override
    public List<MsgFansMomentSopVO> list(MsgFansMomentSopQuery query) {
        List<MsgFansMomentSopDO> entityDOS = msgFansMomentSopMapper.selectList(new LambdaQueryWrapper<MsgFansMomentSopDO>()
                .eq(query.getId() != null, MsgFansMomentSopDO::getId, query.getId())
                .like(StringUtils.isNotBlank(query.getName()), MsgFansMomentSopDO::getName, query.getName())
                .eq(query.getSendStatus() != null, MsgFansMomentSopDO::getSendStatus, query.getSendStatus())
                .orderByDesc(MsgFansMomentSopDO::getCreateTime)
        );
        List<MsgFansMomentSopVO> entityVOS = new ArrayList<>();
        if(Collections3.isNotEmpty(entityDOS)) {
            String tenantId = UserUtils.getTenantId();
            List<CopEmployeeDO> allEmployeeDOS = copEmployeeMapper.selectList(new LambdaQueryWrapper<CopEmployeeDO>());
            List<FriendsDO> allFriendsDOS = friendsMapper.selectList(new LambdaQueryWrapper<FriendsDO>());
            Map<Long, String> employeeIdMapName = Collections3.isNotEmpty(allEmployeeDOS)?allEmployeeDOS.stream().collect(Collectors.toMap(CopEmployeeDO::getId, CopEmployeeDO::getEmployeeName, (o, n) -> n)):null;
            Map<Long, String> friendsIdMapName = Collections3.isNotEmpty(allFriendsDOS)?allFriendsDOS.stream().collect(Collectors.toMap(FriendsDO::getId, FriendsDO::getName, (o, n) -> n)):null;
            entityVOS = entityDOS.stream().map(x -> {
                MsgFansMomentSopVO msgFansMomentSopVO = BeanUtil.convertBean(x,MsgFansMomentSopVO.class);

                String sendRuleStr = x.getSendRule();
                List<String> sendEmployeeList = new ArrayList<>();
                List<String> sendScopeList = new ArrayList<>();
                if(StringUtils.isNotEmpty(sendRuleStr)){
                    MsgSendRuleBO msgSendRuleBO = JsonUtils.fromJson(sendRuleStr, MsgSendRuleBO.class);
                    if(msgSendRuleBO != null) {
                        msgFansMomentSopVO.setMsgSendRule(msgSendRuleBO);
                        List<String> selectedEmployeeCodeList = msgSendRuleBO.getEmployeeCodeList();
                        if(Collections3.isNotEmpty(selectedEmployeeCodeList)){
                            selectedEmployeeCodeList.stream().forEach(d->{
                                String employeeName = employeeIdMapName!=null?employeeIdMapName.get(Long.valueOf(d)):"";
                                if(StringUtils.isNotEmpty(employeeName)) {
                                    sendEmployeeList.add(employeeName);
                                }
                            });
                        }
                        if(msgSendRuleBO.getIsAllFriends() == 1){
                            sendScopeList.add("所有客户");
                        }else{
                            List<Long> friendsTagList = msgSendRuleBO.getFriendsTagsList();
                            if(Collections3.isNotEmpty(friendsTagList)){
                                List<FriendsTagsRelDO> allFriendsTagsRelDOS = friendsTagsRelMapper.listByTagsIds(Integer.valueOf(tenantId), new HashSet<>(friendsTagList));
                                if(Collections3.isNotEmpty(allFriendsTagsRelDOS)){
                                    allFriendsTagsRelDOS.stream().forEach(d->{
                                        String friendsName = friendsIdMapName.get(d.getFriendsId());
                                        sendScopeList.add(friendsName);
                                    });
                                }
                            }
                        }
                    }
                }else{
                    MsgSendRuleBO msgSendRuleBO = new MsgSendRuleBO();
                    msgFansMomentSopVO.setMsgSendRule(msgSendRuleBO);
                }
                if(Collections3.isNotEmpty(sendEmployeeList)){
                    msgFansMomentSopVO.setSendEmployee(StringUtils.join(sendEmployeeList.toArray(),";"));
                }
                if(Collections3.isNotEmpty(sendScopeList)){
                    msgFansMomentSopVO.setSendScope(StringUtils.join(sendScopeList.toArray(),";"));
                }
                msgFansMomentSopVO.setSendStatusDesc(WxCpMessageAssignStatusEnum.getDescByCode(x.getSendStatus()));
                return msgFansMomentSopVO;
            }).collect(Collectors.toList());
        }
        return entityVOS;
    }

    @Override
    public Page<MsgFansMomentSopVO> pageList(MsgFansMomentSopPageQuery pageQuery) {
        LambdaQueryWrapper<MsgFansMomentSopDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageQuery.getId() != null, MsgFansMomentSopDO::getId, pageQuery.getId())
                .like(StringUtils.isNotBlank(pageQuery.getName()), MsgFansMomentSopDO::getName, pageQuery.getName())
                .eq(pageQuery.getSendStatus() != null, MsgFansMomentSopDO::getSendStatus, pageQuery.getSendStatus())
                .orderByDesc(MsgFansMomentSopDO::getCreateTime);
        Page<MsgFansMomentSopDO> entityDOPages = msgFansMomentSopMapper.selectPage(new Page<MsgFansMomentSopDO>(pageQuery.getPageNum(), pageQuery.getPageSize()), wrapper);
        Page<MsgFansMomentSopVO> entityVOPages = BeanUtil.convertPage(entityDOPages, MsgFansMomentSopVO.class);
        if(entityVOPages!=null && entityVOPages.getSize()>0) {
            entityVOPages.getRecords().forEach(f -> {
                String sendRuleStr = f.getSendRule();
                if(StringUtils.isNotEmpty(sendRuleStr)){
                    MsgSendRuleBO msgSendRuleBO = JsonUtils.fromJson(sendRuleStr, MsgSendRuleBO.class);
                    f.setMsgSendRule(msgSendRuleBO);
                }else{
                    MsgSendRuleBO msgSendRuleBO = new MsgSendRuleBO();
                    f.setMsgSendRule(msgSendRuleBO);
                }
                f.setSendStatusDesc(WxCpMessageAssignStatusEnum.getDescByCode(f.getSendStatus()));
            });
        }
        return entityVOPages;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(MsgFansMomentSopParam param) {
        checkUnique(param);
        MsgFansMomentSopDO entityDO = BeanUtil.convertBean(param, MsgFansMomentSopDO.class);
        Long sopId = GenerateId.generateId();
        entityDO.setId(sopId);
        entityDO.setCreateBy(UserUtils.getUserId());
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        MsgSendRuleBO msgSendRuleBO = param.getMsgSendRule();
        if(msgSendRuleBO != null){
            Integer isAllFriends = msgSendRuleBO.getIsAllFriends();
            if(isAllFriends == 1){
                msgSendRuleBO.setFriendsCodeList(null);
                msgSendRuleBO.setFriendsTagsList(null);
            }
            entityDO.setSendRule(JsonUtils.toJsonStr(msgSendRuleBO));
        }
        List<MsgFansMomentSopDetailParam> messageSopDetailParamList= param.getFansMomentSopDetails();
        if(Collections3.isNotEmpty(messageSopDetailParamList)){
            List<MsgFansMomentSopDetailDO> msgFansMomentSopDetailDOS = new ArrayList<>();
            List<CopAttachementParam> copAttachementParams = new ArrayList<>();
            for(MsgFansMomentSopDetailParam detailParam:messageSopDetailParamList){
                MsgFansMomentSopDetailDO msgFansMomentSopDetailDO = BeanUtil.convertBean(detailParam, MsgFansMomentSopDetailDO.class);
                Long sopDetailId = GenerateId.generateId();
                msgFansMomentSopDetailDO.setId(sopDetailId);
                msgFansMomentSopDetailDO.setSopId(sopId);
                msgFansMomentSopDetailDO.setCreateBy(UserUtils.getUserId());
                msgFansMomentSopDetailDO.setCreateTime(LocalDateTime.now());
                msgFansMomentSopDetailDO.setModifyTime(LocalDateTime.now());
                String sendDate = detailParam.getSendDate();
                String sendTime = detailParam.getSendTime();
                if(StringUtils.isNotEmpty(sendDate) && StringUtils.isNotEmpty(sendTime)){
                    LocalDateTime sendDateTime = DateUtil.strToLocalDateTime(sendDate.concat(" ").concat(sendTime));
                    msgFansMomentSopDetailDO.setSendDateTime(sendDateTime);
                }
                List<UniAttachParam> attachParamList = detailParam.getAttachements();
                if(Collections3.isNotEmpty(attachParamList)){
                    CopAttachementParam copAttachementParam = copAttachementServiceImpl.transerUniAttachementToCopAttachement(attachParamList);
                    if(copAttachementParam!=null){
                        copAttachementParam.setMessageId(sopDetailId);
                        copAttachementParams.add(copAttachementParam);
                    }
                }
                msgFansMomentSopDetailDOS.add(msgFansMomentSopDetailDO);
            }
            if(Collections3.isNotEmpty(copAttachementParams)) {
                copAttachementServiceImpl.batchAdd(copAttachementParams);
            }
            if(Collections3.isNotEmpty(msgFansMomentSopDetailDOS)) {
                msgFansMomentSopDetailMapper.insertBatchSomeColumn(msgFansMomentSopDetailDOS);
            }
        }
        return msgFansMomentSopMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(MsgFansMomentSopParam param) {
        Long sopId = param.getId();
        if(sopId == null) throw new BusinessException("获取前端参数出错！");
        MsgFansMomentSopDO entityDO = BeanUtil.convertBean(param, MsgFansMomentSopDO.class);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        MsgSendRuleBO msgSendRuleBO = param.getMsgSendRule();
        if(msgSendRuleBO != null){
            Integer isAllFriends = msgSendRuleBO.getIsAllFriends();
            if(isAllFriends == 1){
                msgSendRuleBO.setFriendsCodeList(null);
                msgSendRuleBO.setFriendsTagsList(null);
            }
            entityDO.setSendRule(JsonUtils.toJsonStr(msgSendRuleBO));
        }
        List<MsgFansMomentSopDetailParam> messageSopDetailParamList= param.getFansMomentSopDetails();
        if(Collections3.isNotEmpty(messageSopDetailParamList)){
            List<Long> needDeleteDetailIds = new ArrayList<>();
            List<Long> needDeleteDetailAttachIds = new ArrayList<>();
            List<CopAttachementParam> copAttachementParams = new ArrayList<>();
            List<MsgFansMomentSopDetailDO> needInsertDetailDOList = new ArrayList<>();
            List<MsgFansMomentSopDetailDO> needUpdateDetailDOList = new ArrayList<>();
            List<MsgFansMomentSopDetailDO> oldMsgFansMomentSopDetailDOS = msgFansMomentSopDetailMapper.selectList(new LambdaQueryWrapper<MsgFansMomentSopDetailDO>().eq(MsgFansMomentSopDetailDO::getSopId,sopId));
            List<Long> allOldDetailIdList = Collections3.isNotEmpty(oldMsgFansMomentSopDetailDOS)?oldMsgFansMomentSopDetailDOS.stream().map(MsgFansMomentSopDetailDO::getId).collect(Collectors.toList()):null;
            List<Long> allNewDetailIdList = messageSopDetailParamList.stream().map(MsgFansMomentSopDetailParam::getId).collect(Collectors.toList());
            if(Collections3.isNotEmpty(allOldDetailIdList)){
                HashSet<Long> allOldDetailIdSet = new HashSet<>(allOldDetailIdList);
                HashSet<Long> allNewDetailIdSet = new HashSet<>(allNewDetailIdList);
                allOldDetailIdSet.removeAll(allNewDetailIdSet);
                needDeleteDetailIds = new ArrayList<>(allOldDetailIdSet);
                needDeleteDetailAttachIds = new ArrayList<>(allOldDetailIdSet);
            }
            List<MsgFansMomentSopDetailParam> needInsertDetailList =  messageSopDetailParamList.stream().filter(d->(d.getId() == null)).collect(Collectors.toList());
            if(Collections3.isNotEmpty(needInsertDetailList)){
                needInsertDetailList.stream().forEach(d->{
                    MsgFansMomentSopDetailDO msgFansMomentSopDetailDO = BeanUtil.convertBean(d,MsgFansMomentSopDetailDO.class);
                    Long sopDetailId = GenerateId.generateId();
                    msgFansMomentSopDetailDO.setId(sopDetailId);
                    msgFansMomentSopDetailDO.setSopId(sopId);
                    msgFansMomentSopDetailDO.setCreateBy(UserUtils.getUserId());
                    msgFansMomentSopDetailDO.setCreateTime(LocalDateTime.now());
                    msgFansMomentSopDetailDO.setModifyTime(LocalDateTime.now());
                    String sendDate = d.getSendDate();
                    String sendTime = d.getSendTime();
                    if(StringUtils.isNotEmpty(sendDate) && StringUtils.isNotEmpty(sendTime)){
                        LocalDateTime sendDateTime = DateUtil.strToLocalDateTime(sendDate.concat(" ").concat(sendTime));
                        msgFansMomentSopDetailDO.setSendDateTime(sendDateTime);
                    }
                    List<UniAttachParam> attachParamList = d.getAttachements();
                    if(Collections3.isNotEmpty(attachParamList)){
                        CopAttachementParam copAttachementParam = copAttachementServiceImpl.transerUniAttachementToCopAttachement(attachParamList);
                        if(copAttachementParam!=null){
                            copAttachementParam.setMessageId(sopDetailId);
                            copAttachementParams.add(copAttachementParam);
                        }
                    }
                    needInsertDetailDOList.add(msgFansMomentSopDetailDO);
                });
            }
            List<MsgFansMomentSopDetailParam> needUpdateDetailList =  messageSopDetailParamList.stream().filter(d->(d.getId() != null)).collect(Collectors.toList());
            if(Collections3.isNotEmpty(needUpdateDetailList)){
                for(MsgFansMomentSopDetailParam d:needUpdateDetailList){
                    MsgFansMomentSopDetailDO msgFansMomentSopDetailDO = BeanUtil.convertBean(d,MsgFansMomentSopDetailDO.class);
                    Long sopDetailId = d.getId();
                    msgFansMomentSopDetailDO.setSopId(sopId);
                    msgFansMomentSopDetailDO.setModifyBy(UserUtils.getUserId());
                    msgFansMomentSopDetailDO.setModifyTime(LocalDateTime.now());
                    String sendDate = d.getSendDate();
                    String sendTime = d.getSendTime();
                    if(StringUtils.isNotEmpty(sendDate) && StringUtils.isNotEmpty(sendTime)){
                        LocalDateTime sendDateTime = DateUtil.strToLocalDateTime(sendDate.concat(" ").concat(sendTime));
                        msgFansMomentSopDetailDO.setSendDateTime(sendDateTime);
                    }
                    List<UniAttachParam> attachParamList = d.getAttachements();
                    if(Collections3.isNotEmpty(attachParamList)){
                        CopAttachementParam copAttachementParam = copAttachementServiceImpl.transerUniAttachementToCopAttachement(attachParamList);
                        if(copAttachementParam!=null){
                            copAttachementParam.setId(IdGenerator.getId());
                            copAttachementParam.setMessageId(sopDetailId);
                            copAttachementParams.add(copAttachementParam);
                        }
                    }
                    needDeleteDetailAttachIds.add(sopDetailId);
                    needUpdateDetailDOList.add(msgFansMomentSopDetailDO);
                };
            }
            if(Collections3.isNotEmpty(needDeleteDetailAttachIds)){
                copAttachementMapper.delete(new LambdaQueryWrapper<CopAttachementDO>().in(CopAttachementDO::getMessageId,needDeleteDetailAttachIds));
            }
            if(Collections3.isNotEmpty(copAttachementParams)){
                copAttachementServiceImpl.batchAdd(copAttachementParams);
            }
            if(Collections3.isNotEmpty(needDeleteDetailIds)){
                msgFansMomentSopDetailMapper.deleteBatchIds(needDeleteDetailIds);
            }
            if(Collections3.isNotEmpty(needUpdateDetailDOList)){
                msgFansMomentSopDetailServiceImpl.updateBatch(needUpdateDetailDOList);
            }
            if(Collections3.isNotEmpty(needInsertDetailDOList)){
                msgFansMomentSopDetailMapper.insertBatchSomeColumn(needInsertDetailDOList);
            }
        }
        return msgFansMomentSopMapper.updateById(entityDO) > 0;
    }

    @Override
    public MsgFansMomentSopVO view(Long id) {
        MsgFansMomentSopDO entityDO = msgFansMomentSopMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        MsgFansMomentSopVO entityVO = BeanUtil.convertBean(entityDO, MsgFansMomentSopVO.class);
        if(entityVO != null) {
            //LocalDateTime sendDateTime = entityDO.;
            String sendRuleStr = entityVO.getSendRule();
            if(StringUtils.isNotEmpty(sendRuleStr)){
                MsgSendRuleBO msgSendRuleBO = JsonUtils.fromJson(sendRuleStr, MsgSendRuleBO.class);
                entityVO.setMsgSendRule(msgSendRuleBO);
            }else{
                MsgSendRuleBO msgSendRuleBO = new MsgSendRuleBO();
                entityVO.setMsgSendRule(msgSendRuleBO);
            }
            List<MsgFansMomentSopDetailDO>  messageSopDetailDOList = msgFansMomentSopDetailMapper.selectList(new LambdaQueryWrapper<MsgFansMomentSopDetailDO>().eq(MsgFansMomentSopDetailDO::getSopId, id));
            if(Collections3.isNotEmpty(messageSopDetailDOList)){
               List<MsgFansMomentSopDetailVO> messageSopDetails = messageSopDetailDOList.stream().map(d->{
                   MsgFansMomentSopDetailVO msgFansMomentSopDetailVO = BeanUtil.convertBean(d, MsgFansMomentSopDetailVO.class);
                   LocalDateTime sendDateTime = d.getSendDateTime();
                   String sendDate = DateUtil.parseStr(sendDateTime,DATE_FORMAT_YYYYMMDD);
                   String sendTime = DateUtil.parseStr(sendDateTime,DATE_FORMAT_HHMMSS);
                   msgFansMomentSopDetailVO.setSendDate(sendDate);
                   msgFansMomentSopDetailVO.setTabTitle(sendDate);
                   msgFansMomentSopDetailVO.setSendTime(sendTime);
                   CopAttachementVO copAttachementVO = copAttachementServiceImpl.queryByMessageId(d.getId());
                   if(null != copAttachementVO) {
                       List<UniAttachVO> uniAttachVOS = copAttachementServiceImpl.transerCopAttachementToUniAttachement(copAttachementVO);
                       msgFansMomentSopDetailVO.setAttachements(uniAttachVOS);
                   }
                   return msgFansMomentSopDetailVO;
               }).collect(Collectors.toList());
                entityVO.setFansMomentSopDetails(messageSopDetails);
            }
            entityVO.setSendStatusDesc(WxCpMessageAssignStatusEnum.getDescByCode(entityDO.getSendStatus()));
        }
        return entityVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeStatus(Long id, Integer status) {
        MsgFansMomentSopDO entityDO = msgFansMomentSopMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        entityDO.setSendStatus(status);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return msgFansMomentSopMapper.updateById(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        MsgFansMomentSopDO entityDO = msgFansMomentSopMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        List<MsgFansMomentSopDetailDO> msgFansMomentSopDetailDOS = msgFansMomentSopDetailMapper.selectList(new LambdaQueryWrapper<MsgFansMomentSopDetailDO>().eq(MsgFansMomentSopDetailDO::getSopId, id));
        if(Collections3.isNotEmpty(msgFansMomentSopDetailDOS)){
            List<Long> detailIds = msgFansMomentSopDetailDOS.stream().map(MsgFansMomentSopDetailDO::getId).collect(Collectors.toList());
            msgFansMomentSopDetailMapper.deleteBatchIds(detailIds);
            copAttachementMapper.delete(new LambdaQueryWrapper<CopAttachementDO>().in(CopAttachementDO::getMessageId,detailIds));
        }
        return msgFansMomentSopMapper.deleteById(id) > 0;
    }
    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(MsgFansMomentSopParam param) {
        if (param == null) {
            throw new BusinessException("当前记录不存在!");
        }
        //code name确保等级唯一性
        LambdaQueryWrapper<MsgFansMomentSopDO> queryWrapper = new LambdaQueryWrapper<MsgFansMomentSopDO>()
                .ne(param.getId() != null, MsgFansMomentSopDO::getId, param.getId())
                .eq(MsgFansMomentSopDO::getName, param.getName());
        List<MsgFansMomentSopDO> dos = msgFansMomentSopMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在同名的记录!");
        }
    }

}
